<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Worklinkstatistic extends Model
{
    protected $table = 'work_link_statistic';
    protected $primaryKey = 'id';

    public static function getWorkLinksStatistic($kfid,$phase)
    {
        return self::select("id")->where("kfid",$kfid)->where("phase",$phase)->first();
    }

    public static function creatWorkLinkStatistic($info)
    {
        return self::insert($info);
    }

    public static function getWorkLinksStatistics($kfid,$phase)
    {
        $query = self::select("work_lists_statistic.*","work_link.title","work_link.users_id","work_link.limit_pv")->leftJoin('work_link', 'work_lists_statistic.kfid', '=', 'work_link.kfid');
        if (!empty($kfid)) $query->where("work_lists_statistic.kfid",$kfid);
        if (!empty($phase)) $query->where("work_lists_statistic.phase",$phase);
        return $query->orderBy("links_lists.id","DESC")->paginate(25);
    }

    public static function getWorkLinksOldStatistics($kfid)
    {
        return DB::select('select sum(page_view) as page_view,sum(mini_view) as mini_view,sum(mini_activate) as mini_activate from work_lists_statistic where kfid = :kfid', ['kfid' => $kfid]);
    }

    public static function getDailyWorkLinksStatistics($kfid,$phases)
    {
        return self::where("kfid",$kfid)->whereIn("phase",$phases)->orderBy("id","DESC")->take(30)->get();
    }

}
